package dao.impl;

import dao.OrderDao;
import entity.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/7 19:26
 */
public class OrderDaoImpl implements OrderDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 添加订单
     *
     * @param orders
     */
    @Override
    public void addOrders(Orders orders) {
        String sql = "insert into orders(o_id,u_id,a_id,o_count,o_time,o_state) values(?,?,?,?,?,?)";
        template.update(sql, orders.getOid(), orders.getUid(), orders.getAid(), orders.getOcount(), orders.getOtime(), orders.getOstate());
    }

    /**
     * 添加订单项
     *
     * @param items
     */
    @Override
    public void addItems(List<Item> items) {
        String sql = "insert into item(p_id,o_id,i_count,i_num) values(?,?,?,?);";
        int sum = 0;
        for (Item item : items) {
            int res = template.update(sql, item.getPid(), item.getOid(), item.getIcount(), item.getInum());
            sum += res;
            System.out.println("res:" + res);
        }
        System.out.println("一共插入了sum条数据:" + sum);


    }

    /**
     * 查询订单集合
     *
     * @param uid
     * @return
     */
    @Override
    public List<Orders> findOrdersByUid(int uid) throws InvocationTargetException, IllegalAccessException {
        String sql = "select o.o_id oid,o.u_id uid,o.a_id aid,o.o_count ocount,o.o_time otime,o.o_state ostate," +
                "a.a_name aname,a.a_phone aphone,a.a_detail adetail,a.a_state astate " +
                "from orders o join address a on a.a_id = o.a_id where o.u_id = ?;";
        List<Map<String, Object>> list = template.queryForList(sql, uid);

        //返回list为空直接返回
        if (list == null) {
            return null;
        }
        //在内存中创建一个装cart对象的容器
        List<Orders> ordersList = new ArrayList<>();

        //遍历查询返回的list集合
        for (Map<String, Object> map : list) {
            //返回不为空，装对象
            Orders orders = new Orders();
            Address address = new Address();

            BeanUtils.populate(orders, map);
            BeanUtils.populate(address, map);

            orders.setAddress(address);

            //装进集合
            ordersList.add(orders);

        }

        return ordersList;
    }

    /**
     * 根据oid查询订单对象
     *
     * @param oid
     * @return
     */
    @Override
    public Orders findOrdersByOid(String oid) throws InvocationTargetException, IllegalAccessException {
        String sql = "select o.o_id oid,o.u_id uid,o.a_id aid,o.o_count ocount,o.o_time otime,o.o_state ostate," +
                "a.a_name aname,a.a_phone aphone,a.a_detail adetail,a.a_state astate " +
                "from orders o join address a on a.a_id = o.a_id where o.o_id = ?;";
        Map<String, Object> map = template.queryForMap(sql, oid);

        //返回list为空直接返回
        if (map == null) {
            return null;
        }
        //在内存中创建一个装cart对象的容器
        List<Orders> ordersList = new ArrayList<>();

        //遍历查询返回的list集合
        //返回不为空，装对象
        Orders orders = new Orders();
        Address address = new Address();

        BeanUtils.populate(orders, map);
        BeanUtils.populate(address, map);

        orders.setAddress(address);

        //装进集合
        ordersList.add(orders);


        return orders;
    }

    /**
     * 根据oid查询订单项
     *
     * @param oid
     * @return
     */
    @Override
    public List<Item> findItemsByOid(String oid) throws InvocationTargetException, IllegalAccessException {

        String sql = "select p.p_id pid,p.t_id tid,p.p_name pname,p.p_time ptime," +
                "p.p_image pimage,p.p_price pprice,p.p_state pstate,p.p_info pinfo," +
                " i.o_id oid,i.i_id iid,i.i_count icount,i.i_num inum" +
                " from product p join item i on p.p_id = i.p_id where i.o_id =?";

        List<Map<String, Object>> list = template.queryForList(sql, oid);
        if (list == null) {
            return null;
        }
        //存放item的容器作为返回值
        List<Item> items = new ArrayList<>();

        for (Map<String, Object> map : list) {
            Product product = new Product();
            Item item = new Item();
            BeanUtils.populate(product, map);
            BeanUtils.populate(item, map);
            item.setProduct(product);
            items.add(item);
        }
        return items;
    }

    @Override
    public void updateOrderStateByOid(String oid) {
        String sql = "update orders set o_state = 2 where o_id = ?";
        template.update(sql, oid);
    }


}
